//
//  SWDetailViewController.m
//  ShiWu
//
//  Created by liujianlong on 2022/7/28.
//

#import "SWDetailViewController.h"
#import <WebKit/WebKit.h>

@interface SWDetailViewController ()<WKNavigationDelegate>
@property (strong, nonatomic) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@end
@implementation SWDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.navigationDelegate = self;
    self.title = LS(@"百度百科");
    //    self.navigationController.navigationBar.hidden = YES;
    
    
    if (self.model.name.length > 0) {
        self.name.text = self.model.name;
        self.content.text = self.model.info;
        self.img.image = [[UIImage alloc] initWithData:self.model.imgdata];
    }else {
        NSString *keyword = [self.dict valueForKey:@"keyword"];
        NSString *name = [self.dict valueForKey:@"name"];
        self.name.text = keyword != nil || [keyword length] > 0 ? keyword : name;
    }
    
    if (self.IMG) {
        self.img.image = self.IMG;
        self.model.type = (int)[CCHttp http].shiwutype;
        self.model.imgdata = UIImagePNGRepresentation(self.IMG);
        self.model.name = self.name.text;
        self.content.text = [[self.dict valueForKey:@"baike_info"] valueForKey:@"description"];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY/MM/dd HH:mm"];
        self.model.time  = [formatter stringFromDate:[NSDate date]];
        BOOL succes = [SWModel bg_saveOrUpdateArray:@[self.model]];
    }
    NSString *str = [NSString stringWithFormat:@"https://baike.baidu.com/item/%@?timestamp=1659179872134",self.name.text];
    NSString *url  = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"GET";
    [self.webView loadRequest:request];
    [RLHud startHud];
}
- (SWModel *)model
{
    if (!_model) {
        _model = [[SWModel alloc] init];
    }
    return  _model;
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (void)backBtnAction
{
    if (self.navigationController.viewControllers.count >= 2) {
        [super backBtnAction];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (IBAction)actionForBack:(UIButton *)sender {
    [self backBtnAction];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    [RLHud startHud];
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [RLHud stopHud];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [RLHud startHud:LS(@"加载失败") stopTime:1 handle:nil];
    
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
    [RLHud  startHud];
}

// 跳转失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
